/// <reference path="../game/fourkill.util.js" />

var SpriteSheetPainter = Base.extend({
    initialize: function initialize() {
        this.SHADOW_COLOR = "rgba(0, 0, 0, 0.3)";
        this.spriteSheet = SpriteSheets.Bird;
    },
    paint: function paint(context) {
        var location = context.gameObject.locationRelativeToCamera,
            rect = location.objectRectangle,
            shRect = location.shadowRectangle,
            vp = context.viewPort,
            go = context.gameObject;
        
        if (location.visible)
        {
            if (!(shRect.x > vp.size.w || shRect.y > vp.size.h ||
                shRect.getRight() < 0 || shRect.getBottom() < 0))
            {
                context.backLayer.fillStyle = this.SHADOW_COLOR;
                util.fillEllipse(context.backLayer, shRect.x, shRect.y, shRect.w, shRect.h);
            }

            if (rect.x > vp.size.w || rect.y > vp.size.h ||
                rect.getRight() < 0 || rect.getBottom() < 0 ||
                rect.w <= 0 || rect.h <= 0) return;

            //context.frontLayer.fillStyle = context.gameObject.color;
            //context.frontLayer.fillRect(rect.x, rect.y, rect.w, rect.h);

            //this.spriteSheet
            //var spriteState = go.z > 0 ? "fly" : "walk"; // need to look at this!
            var spriteStates = [];
            spriteStates.push((go.speed > 0) ? "moving" : "still");
            spriteStates.push((go.z > 0) ? "fly" : "walk");
            //else spriteState = 0;

            //TODO: need a sprite state! (Above)
            var ss = this.spriteSheet;
            if (ss.isReady()) {
                var fIdx = go.frameIndex, fCount = ss.getFrameCount(spriteStates);
                if (fCount > 1) {
                    if (fIdx >= (fCount - 1))
                        go.frameIncrement = -1;
                    else if (fIdx <= 0)
                        go.frameIncrement = 1;
                    fIdx += go.frameIncrement;
                }
                else fIdx = 0;
                go.frameIndex = fIdx;

                var direction = null;
                if (ss.directions == 8) {
                    direction = util.determine8WayDirection(location.placement.bearing);
                }
                else {
                    direction = util.determine4WayDirection(location.placement.bearing);
                }
                /*pctx.frontLayer.fillStyle = "#F00";
                pctx.frontLayer.fillText(direction.toUpperCase() + " " + util.degrees(location.placement.bearing), rect.x, rect.y);*/

                context.frontLayer.drawImage(ss.image,
                    ss.getXOff(go.frameIndex), ss.getYOff(direction), ss.w, ss.h,
                    rect.x, rect.y, rect.w, rect.h);
            }
        }
    }
});
